package com.qf.web.servlet;

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.sevice.CartService;
import com.qf.sevice.GoodsService;
import com.qf.sevice.Impl.CartServiceImpl;
import com.qf.sevice.Impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:Shopping
 * author:RongChao
 * time:2022/4/19 21:27
 * description:
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {
        CartService cartService = new CartServiceImpl();
        public void clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
            User user = (User) request.getSession().getAttribute("user");
            cartService.clearCart(user.getId());
            response.getWriter().print("success");

        }
        public void addCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
            User user = (User) request.getSession().getAttribute("user");
            String goodsId = request.getParameter("goodsId");
            String number = request.getParameter("number");

            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.findById(Integer.parseInt(goodsId));
            //根据用户id和商品获取获取购物车信息
            Cart cart = cartService.findByUidAndGid(user.getId(), Integer.parseInt(goodsId));
            if (cart == null){
                cart = new Cart();
                cart.setId(user.getId());
                cart.setPid(Integer.parseInt(goodsId));
                cart.setMoney(Integer.parseInt(number) * goods.getPrice());
                cart.setNum(Integer.parseInt(number));
                cartService.insert(cart);
            }else{
                if (Integer.parseInt(number) == 0){
                    cartService.delectCart(user.getId(), Integer.parseInt(goodsId));
                }else {
                    cart.setNum(cart.getNum() + Integer.parseInt(number));
                    cart.setMoney(goods.getPrice() * cart.getNum());
                    cartService.updateCart(cart);
                }
            }
            response.getWriter().print("success");
        }

        public String getCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
            Object user1 = request.getSession().getAttribute("user");
            if (user1 == null){
                return "redirect:/login.jsp";
            }
            User user = (User)user1;
            List<Cart> carts=cartService.findCartByUid(user.getId());
            request.setAttribute("carts",carts);
            return "forward:/cart.jsp";
        }


        public String addCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object userObj = request.getSession().getAttribute("user");
        if (userObj == null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StringUtils.isEmpty(goodsId) || StringUtils.isEmpty(number)){
            request.setAttribute("msg", "添加到购物车失败！！");
            return "forward:/message.jsp";
        }
        User user =(User)userObj;
        //根据商品id 查询商品对象
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));
        //查询该用户有没有添加此商品到购物车， 如果此商品已经在购物车，更新，如果不在，则新增
        Cart cart = cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));
        //购物车不存在就新增
        if (cart == null){
            cart = new Cart();
            cart.setId(user.getId());
            cart.setMoney(Integer.parseInt(number) * goods.getPrice());
            cart.setNum(Integer.parseInt(number));
            cart.setPid(Integer.parseInt(goodsId));
            cartService.insert(cart);
        }else{
            cart.setNum(cart.getNum() + Integer.parseInt(number));
            cart.setMoney(goods.getPrice() * cart.getNum());
            cartService.updateCart(cart);
        }
        return "redirect:/cartSuccess.jsp";
    }
}
